<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="机构名称" prop="business">
      <el-input v-model="dataForm.business" placeholder="机构名称"></el-input>
    </el-form-item>
    <el-form-item label="二维码" prop="qrCode">
      <el-input v-model="dataForm.qrCode" placeholder="二维码"></el-input>
    </el-form-item>
    <el-form-item label="是否全预约 0-不需要，1-需要" prop="isAppoint">
      <el-input v-model="dataForm.isAppoint" placeholder="是否全预约 0-不需要，1-需要"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="appId">
      <el-input v-model="dataForm.appId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="appSecret">
      <el-input v-model="dataForm.appSecret" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="教练端消息提醒" prop="templateTeacher">
      <el-input v-model="dataForm.templateTeacher" placeholder="教练端消息提醒"></el-input>
    </el-form-item>
    <el-form-item label="学生端消息提醒" prop="templateStudent">
      <el-input v-model="dataForm.templateStudent" placeholder="学生端消息提醒"></el-input>
    </el-form-item>
    <el-form-item label="审核状态" prop="isPass">
      <el-input v-model="dataForm.isPass" placeholder="审核状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="wxpayAppId">
      <el-input v-model="dataForm.wxpayAppId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="wxpayMachId">
      <el-input v-model="dataForm.wxpayMachId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="wxpayKey">
      <el-input v-model="dataForm.wxpayKey" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          business: '',
          qrCode: '',
          isAppoint: '',
          status: '',
          appId: '',
          appSecret: '',
          createTime: '',
          updateTime: '',
          templateTeacher: '',
          templateStudent: '',
          isPass: '',
          wxpayAppId: '',
          wxpayMachId: '',
          wxpayKey: ''
        },
        dataRule: {
          business: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          qrCode: [
            { required: true, message: '二维码不能为空', trigger: 'blur' }
          ],
          isAppoint: [
            { required: true, message: '是否全预约 0-不需要，1-需要不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          appSecret: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          templateTeacher: [
            { required: true, message: '教练端消息提醒不能为空', trigger: 'blur' }
          ],
          templateStudent: [
            { required: true, message: '学生端消息提醒不能为空', trigger: 'blur' }
          ],
          isPass: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ],
          wxpayAppId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          wxpayMachId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          wxpayKey: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/business/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.business = data.business.business
                this.dataForm.qrCode = data.business.qrCode
                this.dataForm.isAppoint = data.business.isAppoint
                this.dataForm.status = data.business.status
                this.dataForm.appId = data.business.appId
                this.dataForm.appSecret = data.business.appSecret
                this.dataForm.createTime = data.business.createTime
                this.dataForm.updateTime = data.business.updateTime
                this.dataForm.templateTeacher = data.business.templateTeacher
                this.dataForm.templateStudent = data.business.templateStudent
                this.dataForm.isPass = data.business.isPass
                this.dataForm.wxpayAppId = data.business.wxpayAppId
                this.dataForm.wxpayMachId = data.business.wxpayMachId
                this.dataForm.wxpayKey = data.business.wxpayKey
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/business/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'business': this.dataForm.business,
                'qrCode': this.dataForm.qrCode,
                'isAppoint': this.dataForm.isAppoint,
                'status': this.dataForm.status,
                'appId': this.dataForm.appId,
                'appSecret': this.dataForm.appSecret,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'templateTeacher': this.dataForm.templateTeacher,
                'templateStudent': this.dataForm.templateStudent,
                'isPass': this.dataForm.isPass,
                'wxpayAppId': this.dataForm.wxpayAppId,
                'wxpayMachId': this.dataForm.wxpayMachId,
                'wxpayKey': this.dataForm.wxpayKey
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
